package com.example.controller;


import com.example.common.Result;
import com.example.entity.Introduction;
import com.example.service.IntroductionService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/introduction")
public class IntroductionContoller {
    @Resource
    IntroductionService introductionService;

    @PostMapping("/add")
    public Result add(@RequestBody Introduction introduction){ //@RequestBody: 表示将请求体中的json数据转换为Admin对象
        introductionService.add(introduction);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){ //@PathVariable: 表示从路径中获取参数
        introductionService.delete(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Introduction introduction){ //@RequestBody: 表示将请求体中的json数据转换为Admin对象
        introductionService.update(introduction);
        return Result.success();
    }

    @GetMapping("/selectAll")  //  接口的路径
    public Result selectAll(Introduction introduction){
        List<Introduction> introductionList = introductionService.selectAll(introduction);
        return Result.success(introductionList);

    }

    /*
     *   分页查询
     * pageNum: 当前页码
     * pageSize: 每页的个数
     * */
    @GetMapping("/selectPage")// 接口的路径
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             // @RequestParam(required = false) String name
                             Introduction introduction
    ) {

        PageInfo<Introduction> pageInfo = introductionService.selectPage(pageNum, pageSize, introduction);
        return Result.success(pageInfo); //返回的是分页的对象
    }
    /*
    * 根据id查询
    * */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Introduction introduction = introductionService.selectById(id);
        return Result.success(introduction);
    }

}
